const app = getApp()
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
Page({
  /**
      * 页面的初始数据
      */
  data: {
    carinfo: {},
  },
  onLoad: function (options) {
    wx.request({
      url: base_url + '/Users/getMyCarInfo',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == '1') {
          var nick_name = res.data.data.wx_nick_name;
          // console.log(util.strchangdu(res.data.data.wx_nick_name));
          var len = 0;
          for (var i = 0; i < nick_name.length; i++) {
            var c = nick_name.charCodeAt(i);
            //单字节加1 
            if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
              len++;
            }
            else {
              len += 2;
            }
          }

          if(len>15){
            nick_name = nick_name.substr(0,15);
            nick_name =  nick_name+'...';
          }
          res.data.data.wx_nick_name = nick_name;


          this.setData({
            carinfo: res.data.data,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          }),
            wx.navigateBack({
              delta: 1
            })
        }
      }
    })
  },
  editInfo: function (event) {
    wx.navigateTo({
      url: '../editInfo/index'
    })
  },
  addCar:function(event){
    wx.scanCode({
      onlyFromCamera: true,
      success: (res) => {
        console.log(res)
        var src = res.result;
        src = src.substr(52);
        this.topage(src);


        // wx.redirectTo({
        //   url: '../carInfo/index?q=' + src
        // })
      }
    })
  },
  topage: function (id) {
    var user_token = (wx.getStorageSync('user_token'))
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Qcode/index',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'id': id
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            console.log(res)
            if (res.data.success == '1') {
              if (res.data.code == '0') {
                wx.navigateTo({
                  url: '../carInfo/index?id=' + id
                })
              } else if (res.data.code == '800') {
                wx.navigateTo({
                  url: '../bindPhone/index?id=' + id
                })
              } else if (res.data.code == '801') {
                wx.navigateTo({
                  url: '../myInfo/index'
                })
              }
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }
          }
        })
      }
    })
  }
})
